﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Attendance.AttendanceClassDate;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceClassDate
{
    public class AttendanceClassDateData : IAttendanceClassDateData
    {
        private IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT AttendanceClassDeteId,GroupId,ClassId,ClassName,Start,End,Year,Month,Day,DayOfWeek,EffectiveDate 
                                        FROM OA_AttendanceClassDate";

        public AttendanceClassDateData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        /// <summary>
        /// 根据日期获取当前排班信息
        /// </summary>
        /// <param name="groupId">考勤组Id</param>
        /// <returns></returns>
        public async Task<AttendanceClassDateEntity> GetLatestEntityByGroupIdAndEffectiveDate(long groupId, DateTime effectiveDate)
        {
            string sql = string.Format("{0} WHERE  GroupId=?GroupId AND EffectiveDate=?EffectiveDate ORDER BY AttendanceClassDeteId DESC  LIMIT 1;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GroupId", groupId, DbType.Int64);
            parameters.Add("?EffectiveDate", effectiveDate.Date, DbType.Date);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceClassDateEntity>(sql, parameters);
        }

        #region 新增操作

        /// <summary>
        /// 新增一组排班日期表
        /// </summary>
        /// <param name="entities">排班日期信息</param>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceClassDateEntity>> InsertListAsync(List<AttendanceClassDateEntity> entities)
        {
            string sql = SqlCommands.GetInsertSql("OA_AttendanceClassDate", new string[] { "GroupId", "ClassId", "ClassName", "`Start`", "`End`", "`Year`", "`Month`",
                "`Day`", "DayOfWeek", "EffectiveDate" });
            List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();
            foreach (var item in entities)
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("?Year", item.Year, DbType.Int32);
                parameters.Add("?Month", item.Month, DbType.Int32);
                parameters.Add("?Day", item.Day, DbType.Int32);
                parameters.Add("?GroupId", item.GroupId, DbType.Int64);
                parameters.Add("?ClassId", item.ClassId, DbType.Int64);
                parameters.Add("?ClassName", item.ClassName, DbType.AnsiString, size: 50);
                parameters.Add("?Start", item.Start, DbType.AnsiString, size: 50);
                parameters.Add("?End", item.End, DbType.AnsiString, size: 50);
                parameters.Add("?DayOfWeek", item.DayOfWeek, DbType.AnsiString, size: 50);
                parameters.Add("?EffectiveDate", item.EffectiveDate, DbType.Date);
                dynamicParameters.Add(parameters);
            }
            var executeCount = await this.databaseContext.Master.ExecuteAsync(sql, dynamicParameters, databaseContext.Transaction);
            if (executeCount > 0)
            {
                sql = string.Format("{0} WHERE GroupId=?GroupId AND Year=?Year AND FIND_IN_SET(Month,?Month)", QUERY);
                DynamicParameters parameter = new DynamicParameters();
                parameter.Add("?GroupId", entities.First().GroupId, DbType.Int64);
                parameter.Add("?Year", DateTime.Now.Year, DbType.Int32);
                parameter.Add("?Month", string.Join(",", new long[] { DateTime.Now.Month, DateTime.Now.AddMonths(1).Month }), DbType.AnsiString);
                return await this.databaseContext.Master.QueryAsync<AttendanceClassDateEntity>(sql, parameter);
            }
            return new List<AttendanceClassDateEntity>();
        }
        #endregion

        /// <summary>
        ///获取指定班次下的排班日期列表
        /// </summary>
        /// <param name="groupId">考勤组Id</param>
        /// <param name="effectiveDate">考勤组生效日期</param>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceClassDateEntity>> GetDateEntitiesByGroupIdAndEffectiveDate(long groupId, DateTime effectiveDate)
        {
            string sql = string.Format("{0} WHERE GroupId=?GroupId AND EffectiveDate=?EffectiveDate;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GroupId", groupId, DbType.Int64);
            parameters.Add("?EffectiveDate", effectiveDate.Date, DbType.Date);
            return await this.databaseContext.Slave.QueryAsync<AttendanceClassDateEntity>(sql, parameters);
        }


    }
}
